

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store.Web.Controls;
using System.Net;

namespace JTC.MiniCommerce.Web.admin.controls {
  public partial class rsslist : AdminControl {

    #region Member Variables

    private string _newsFeedUrl;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        DataSet rssData = RefreshFeed();

        object[] channelItems = rssData.Tables[1].Rows[0].ItemArray;
        int titleColumn = rssData.Tables[1].Columns["title"].Ordinal;
        int descriptionColumn = rssData.Tables[1].Columns["description"].Ordinal;

        lblFeedTitle.Text = channelItems.GetValue(titleColumn).ToString();

        if (rssData.Tables.Count > 2) { // are there any items in the feed?
          Repeater1.DataSource = rssData.Tables[2];
          Repeater1.DataBind();
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(rsslist).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Refreshes the feed.
    /// </summary>
    /// <returns></returns>
    private DataSet RefreshFeed() {
      rssLink.NavigateUrl = HttpUtility.UrlPathEncode(this.NewsFeedUrl);

      HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(this.NewsFeedUrl);

      DataSet rssData = new DataSet();
      rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());

      return rssData;
    }

    #endregion

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the news feed URL.
    /// </summary>
    /// <value>The news feed URL.</value>
    public string NewsFeedUrl {
      get {
        return _newsFeedUrl;
      }
      set {
        _newsFeedUrl = value;
      }
    }

    #endregion

  }
}